package com.xnyzc.lhy.common.exception;


import com.xnyzc.lhy.common.entity.Sb;
import com.xnyzc.lhy.common.penum.EErrorCode;
import lombok.Data;


/**
 * panggj 权限异常
 */
@Data
public class PangException extends RuntimeException {

    // 定义枚举
    private EErrorCode errorCode;
    // 定义描述
    private String meMessage;

    /**
     * 构造函数
     *
     * @param errorCode
     */
    public PangException(EErrorCode errorCode) {
        super(errorCode.toChString());

        this.errorCode = errorCode;
        this.meMessage = errorCode.toChString();
    }

    /**
     * 构造函数
     *
     * @param errorCode
     * @param meMessage
     */
    public PangException(EErrorCode errorCode, String meMessage) {
        super(meMessage);

        this.errorCode = errorCode;
        this.meMessage = meMessage;
    }

    public PangException(Exception e) {
        super(e);

        this.errorCode = EErrorCode.unkown;
        this.meMessage = "";
    }

    public PangException(String meMessage) {
        super(meMessage);

        this.errorCode = EErrorCode.unkown;
        this.meMessage = "";
    }

    /**
     * 构造函数
     *
     * @param errorCode
     * @param meMessage
     */
    public PangException(EErrorCode errorCode, String meMessage, Exception e) {
        super(meMessage, e);

        this.errorCode = errorCode;
        this.meMessage = meMessage;
    }

    /**
     * 构造函数
     *
     * @param meMessage
     */
    public PangException(String meMessage, Exception e) {
        super(meMessage, e);

        this.errorCode = EErrorCode.authErr;
        this.meMessage = meMessage;
    }

    public static PangException create(EErrorCode errorCode) {
        return new PangException(errorCode);
    }

    public static PangException create(EErrorCode errorCode, String meMessage, Object... args) {
        return new PangException(errorCode, Sb.format(meMessage, args));
    }

    public static PangException create(Exception e) {
        return new PangException(e);
    }

    public static PangException create(String meMessage) {
        return new PangException(meMessage);
    }

    public PangException(Integer code,String meMessage) {
        super(meMessage);
        this.errorCode = EErrorCode.bridgeError;
        this.meMessage = meMessage;
    }

    public static PangException create(Integer code,String meMessage) {
        return new PangException(code,meMessage);
    }

    /**
     * 是否skip or repet error
     *
     * @return
     */
    public boolean isSkipOrRepet() {
        return errorCode == EErrorCode.skip || errorCode == EErrorCode.repeat || errorCode == EErrorCode.beUsed;
    }

    /**
     * 打印日志
     *
     * @return
     */
    public String printLog() {
        return new StringBuilder().append("PAuth exception, type:").append(this.errorCode.toChString()).append(" message:").append(this.meMessage)
                .toString();
    }
}
